/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sharingTableModel;

import SessionBeans.Sharing.SharingService;
import entities.SharedFile;
import entities.SharedFolder;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Tomas
 */
public class SharedTableModel {
    SharingService sharingService = lookupSharingServiceBean();

    public SharedTableModel() {
    }

    public List<SharedNodeData> loadData(Long id, boolean folder) {
        if (folder) {
            List<SharedFolder> loadSharedFolder = sharingService.loadSharedFolder(id);
            
            return buildFolderInfo(loadSharedFolder);
        } else {
            List<SharedFile> loadSharedFile = sharingService.loadSharedFile(id);

            return buildFileInfo(loadSharedFile);
        }
    }
    
    public void remove(Long id, boolean folder){
        if(folder){
            sharingService.removeSharedFolder(id);
        }else{
            sharingService.removeSharedFile(id);
        }
    }

    private List<SharedNodeData> buildFileInfo(List<SharedFile> data) {
        List<SharedNodeData> result = new ArrayList<>();

        for (SharedFile sharedFile : data) {
            SharedNodeData sh = new SharedNodeData();
            sh.setId(sharedFile.getId());
            sh.setAssigned(sharedFile.getSharedDate());
            sh.setRights(sharedFile.getRights());
            sh.setUser(sharedFile.getSharedUser());

            result.add(sh);
        }

        return result;
    }

    private List<SharedNodeData> buildFolderInfo(List<SharedFolder> data) {
        List<SharedNodeData> result = new ArrayList<>();

        for (SharedFolder sharedFolder : data) {
            SharedNodeData sh = new SharedNodeData();
            sh.setId(sharedFolder.getId());
            sh.setAssigned(sharedFolder.getSharedDate());
            sh.setRights(sharedFolder.getRights());
            sh.setUser(sharedFolder.getSharedUser());

            result.add(sh);
        }

        return result;
    }

    public void add(Long id, boolean folder, Long ownerUserId, Long sharedUserId, Long rightsId) {
        if (folder) {
            sharingService.saveSharedFolder(ownerUserId, sharedUserId, id, rightsId);
        } else {
            sharingService.saveSharedFile(ownerUserId, sharedUserId, id, rightsId);
        }
    }

    private SharingService lookupSharingServiceBean() {
        try {
            Context c = new InitialContext();
            return (SharingService) c.lookup("java:global/tCloudEE/tCloudEE-ejb/SharingService!SessionBeans.Sharing.SharingService");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
    
    
}
